package com.gary.jstackDemo;

/**
 * @author peng_rui
 * @date 2022/3/12
 */
public class JstackDeadLock {
    public static void main(String[] args) {
        JstackDeadLock lock1 = new JstackDeadLock();
        JstackDeadLock lock2 = new JstackDeadLock();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " 准备获取lock1");
            synchronized (lock1) {
                System.out.println(Thread.currentThread().getName() + " 获取了lock1");
                System.out.println(Thread.currentThread().getName() + " 准备获取了lock2");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock2) {
                    System.out.println(Thread.currentThread().getName() + " 获取了lock2");
                }
            }
        }).start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " 准备获取lock2");
            synchronized (lock2) {
                System.out.println(Thread.currentThread().getName() + " 获取了lock2");
                System.out.println(Thread.currentThread().getName() + " 准备获取了lock1");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock1) {
                    System.out.println(Thread.currentThread().getName() + " 获取了lock2");
                }
            }
        }).start();
    }
}
